package com.leetcode.algorithm.y22.m07.w4;

/**
 * 238. 除自身以外数组的乘积
 * 
 * https://leetcode.cn/problems/product-of-array-except-self/
 * 
 * @author jie.deng
 *
 */
class Question0238Solution03 {

	public int[] productExceptSelf(int[] nums) {
		int len = nums.length;
		if (len == 1) {
			return new int[] { 0 };
		}
		// lArr[i]表示nums[i]左侧数组的乘积
		int[] lArr = new int[len];
		lArr[0] = 1;
		for (int i = 1; i < len; i++) {
			lArr[i] = lArr[i - 1] * nums[i - 1];
		}
		// rArr[i]表示nums[i]右侧数组的乘积
		int[] rArr = new int[len];
		rArr[len - 1] = 1;
		for (int i = len - 2; i >= 0; i--) {
			rArr[i] = rArr[i + 1] * nums[i + 1];
		}

		int[] arr = new int[len];
		for (int i = 0; i < len; i++) {
			arr[i] = lArr[i] * rArr[i];
		}
		return arr;
	}

}